#include <stdio.h>
#include <stdlib.h>

typedef struct Node{
	int num;
	int data;
	struct Node *next;
}NODE,*pNODE;

int addNodes(pNODE head);
pNODE creatLinkList(void);
int putList(pNODE head);

int main(void)
{
	pNODE li = creatLinkList();
	addNodes(li);
	putList(li);

	system("pause");
	return 0;

}
pNODE creatLinkList(void)
{
	pNODE head,tail;
	long length;
	int value,i;

	head = (pNODE)malloc(sizeof(NODE));
	if(NULL == head){
		puts("Dynamic memory malloc failed!,program finish!");
		exit(-1);
	}
	head->next = NULL;
	tail = head;
	printf("Please input the length of linklist:len=");
	scanf("%ld",&length);

	for(i = 0; i<length; i++){
		printf("Please input the %dth node value:",i+1);
		scanf("%d",&value);
		pNODE newNode = (pNODE)malloc(sizeof(NODE));

		newNode->num = i+1;
		newNode->data = value;
		tail->next = newNode;
		newNode->next = NULL;
		tail = newNode;
	}

	
	return head;
}

int addNodes(pNODE head)
{
	pNODE tail = head->next;
	while(tail->next){
		tail = tail->next;
	}//寻尾

	int value;
	puts("Enter the new node value,q to quit");
	while(scanf("%d",&value)&&(value!='q')){
		pNODE newNode = (pNODE)malloc(sizeof(NODE));

		newNode->num = tail->num+1;
		newNode->data = value;//数据域
		
		tail->next = newNode;
		newNode->next = NULL;
		tail = newNode;//tail始终在尾部
		if(newNode->next == NULL){
			puts("Add succeed!\nEnter the new node value,q to quit");
		}
	}
	return 1;
}

int putList(pNODE head)
{
	int i = 1;
	pNODE currentNode = head->next;
	while(currentNode){
		printf("The %5dth node value = %5d\n",currentNode->num,currentNode->data);
		currentNode = currentNode->next;
	}

	return 0;
}

int insertNode(pNODE head,int prevNodeValue)
{
	pNODE currentNode = head;
	while (currentNode->num != prevNodeValue){
		currentNode = currentNode->next;
	}
	pNODE newNode = (pNODE)malloc(sizeof(NODE));
	printf("Please input newnode value:");
	scanf("%d", &newNode->data);

}
